package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TIareacontent;
import com.zmht.beike.po.TIareainfo;
import com.zmht.beike.po.TIweekinfo;
import com.zmht.beike.util.TimeUtil;
import com.zmht.specedu.vo.Teacher;

public class IAreainfoAction extends BaseAction {
	/**
	 * 获得领域目标列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAreainfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		
		List list  = this.getIareainfoBiz().getAreainfo(teacher.getTeacherId(), (index-1)*pageSize, pageSize);
		int total =this.getIareainfoBiz().getAreainfoTotal(teacher.getTeacherId());
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		request.setAttribute("teacher", teacher);
		request.setAttribute("areainfos", list);
		return mapping.findForward("gebielinyu");
	}
	/**
	 * 保存记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveAreainfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//周主题
		TIweekinfo info = (TIweekinfo)request.getSession().getAttribute("iweekinfo");
		this.saveAreaInfo(teacher.getTeacherId(),info.getId(),info.getIaimId());
		
		
		return new ActionForward("/beike/iareainfo.do?method=getAreainfo");
	}
	
	private boolean saveAreaInfo(Integer teacherId,Integer iweekId,Integer iaimid){
		//领域从6---12
		for(int i=6;i<13;i++){
			List<TIareacontent> list = this.getIareaContentBiz().getAreaContent(teacherId, i, 0, 100);
			if(list!=null&&list.size()>0){
				TIareainfo areinfo = new TIareainfo();
				areinfo.setIareaId(i);
				areinfo.setAge(list.get(0).getAge()+"");
				areinfo.setTeacherId(teacherId);
				areinfo.setIweekId(iweekId);
				areinfo.setIaimId(iaimid);
				areinfo.setCtime(TimeUtil.getYMDTime());
				Integer id = this.getIareainfoBiz().saveAreainfo(areinfo);
				for(TIareacontent t:list){
					t.setIareaId(id);
					this.getIareaContentBiz().updateAreaContent(t);
				}
			}
		}
		return false;
	}
	/**
	 * 当编辑目标时 获得内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initAreaInfoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String targetId = request.getParameter("targetId");
		String month = request.getParameter("month");
		String week = request.getParameter("week");
		String areaId = request.getParameter("areaId");
		
		
		request.setAttribute("targetId", targetId);
		request.setAttribute("month", month);
		request.setAttribute("week", week);
		request.setAttribute("areaId", areaId);
		return new ActionForward("/jsp/mubiaozhiding/edit_Gebielinyu.jsp");
	}
	/**
	 * 编辑时 点击保存后
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editAreaInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//直接跳转
		return new ActionForward("/beike/iareainfo.do?method=getAreainfo");
	}
}
